

//
//  AddSendInfoViewController.m
//  LuxuryGoods
//
//  Created by DCQ on 2018/8/5.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "AddSendInfoViewController.h"
#import "CourierListViewController.h"
#import "IdAndMaOrderListViewController.h"
#import "UserInfoModel.h"
#import "ConOrReListViewController.h"
#import "MessageListViewController.h"

@interface AddSendInfoViewController ()
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;
@property (weak, nonatomic) IBOutlet UILabel *kdNameLabel;
@property (weak, nonatomic) IBOutlet UITextField *kdNoTF;
@property (weak, nonatomic) IBOutlet UIView *companyBgV;
@property (copy, nonatomic) NSString * orderId;
@property (assign, nonatomic) AddSendInfoVCType type;
@end

@implementation AddSendInfoViewController
- (instancetype)initWithOrderId:(NSString *)orderId withType:(AddSendInfoVCType)type{
    self = [super init];
    
    if (self) {
        self.orderId = orderId;
        self.type = type;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNavigationItemTitle:@"填写寄件物流"];
    self.sureBtn.layer.masksToBounds = YES;
    self.sureBtn.layer.cornerRadius = 44/2;
    self.sureBtn.layer.borderColor =  [UIColor clearColor].CGColor;
    self.sureBtn.layer.borderWidth = 1;
    self.kdNameLabel.textColor = UIColorFromRGB(0x99999);
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    [self.companyBgV addGestureRecognizer:tap];
    
}
- (void)tapAction{
    
    [self gotoCourierListVC];
}
- (IBAction)sureBtnAction:(id)sender {
    
    [self submitCourierRequest];
}

- (void)showAlertView:(NSString *)msg withConfirmAction:(void (^ __nullable)(UIAlertAction *action))confirmHandler {
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温馨提示"
                                                                   message:msg
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                          handler:confirmHandler];
   
    
    [alert addAction:defaultAction];

    [self presentViewController:alert animated:YES completion:nil];
    
}
- (void)submitCourierRequest{
    if (self.kdNameLabel.text.length == 0) {
        [self showAlertView:@"请选择快递公司" withConfirmAction:nil];
        return;
    }
    if (self.kdNoTF.text.length == 0) {
        [self showAlertView:@"请填写快递单号" withConfirmAction:nil];
        return;
    }
    
    NSDictionary * params = @{@"order_id":self.orderId,@"token":UserInfo.userToken,@"express_name":self.kdNameLabel.text,@"express_no":self.kdNoTF.text};
    
    NSString * pathStr = @"";
    if (self.type == AddSendInfoVCType_idOrMatn) {
        pathStr = @"Otherorder/addExpress";
    }else if (self.type == AddSendInfoVCType_conOrRe){
         pathStr = @"Consignorder/addExpress";
        
    }
    WEAKSELF
    [[MainApi sharedInstance] postPath:pathStr params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
     
        NSLog(@"%@===",responseObject);
        if (responseObject && !error) {
            NSString  *notificationName = @"";
            if (self.fromVCType == AddSendInfoFromVCType_Msg) {
                notificationName = @"UPDATE_MESSAGE_LIST";
            }else{
                if (weakSelf.type == AddSendInfoVCType_conOrRe) {
                    notificationName = @"UPDATE_ConOrReOrder_LIST";
                }else if (weakSelf.type == AddSendInfoVCType_idOrMatn){
                    notificationName = @"UPDATE_IdAndMaOrder_LIST";
                }
            }
            
            [[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:nil];
                
           for (UIViewController * tempVC in self.navigationController.viewControllers) {
                 if ([tempVC isKindOfClass:[IdAndMaOrderListViewController class]]) {
                      [weakSelf.navigationController popToViewController:tempVC animated:YES];
                 }
                if ([tempVC isKindOfClass:[ConOrReListViewController class]]) {
                   [weakSelf.navigationController popToViewController:tempVC animated:YES];
               }
               if ([tempVC isKindOfClass:[MessageListViewController  class]]) {
                   [self.navigationController popToViewController:tempVC animated:YES];
               }
           }
         
        }
 
    }];
    
}
- (void)gotoCourierListVC{
    
    CourierListViewController * courierVC = [[CourierListViewController alloc]init];
    courierVC.chooseCourierBlock = ^(NSDictionary *item) {
        self.kdNameLabel.text = item[@"express_name"];
    };
    [self pushViewController:courierVC];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
